package com.hcb.tonguetiphealth.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hcb.tonguetiphealth.manager.TextViewShowHtmlManager;
import com.hcb.tonguetiphealth.model.DrinksBookDrinkInfoGroup;
import com.hcb.tonguetiphealth.view.R;

public class DrinksBookDrinkInfoExpandableListAdapter extends BaseExpandableListAdapter {

	private List<DrinksBookDrinkInfoGroup> groupList;
	private Context mContext;
    
	public DrinksBookDrinkInfoExpandableListAdapter(Context mContext, List<DrinksBookDrinkInfoGroup> groupList) {
		this.mContext = mContext;
		this.groupList = groupList;
	}
	public Object getChild(int groupPosition, int childPosition) {
		return groupList.get(groupPosition).getChildList().get(childPosition);
	}

 
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	 
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		String child = groupList.get(groupPosition).getChildList().get(childPosition);
        
		View view = convertView;
		
        if(view == null){
            LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.drinks_book_drinks_info_child, null);
        }
		TextView text = (TextView)view.findViewById(R.id.drinks_book_drinks_info_child_name);
		if(groupPosition > 1) {
			text.setText(child);
		} else {
			TextViewShowHtmlManager.show(text, child);
		}
	
		return view;
	}

	public int getChildrenCount(int groupPosition) {
		
		return groupList.get(groupPosition).getChildList().size();
	}

	public Object getGroup(int groupPosition) {
		return groupList.get(groupPosition);
	}

	public int getGroupCount() {
		return groupList.size();
	}

	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		View groupView = convertView;

		if(groupView == null) {
			LayoutInflater inflater_new = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			groupView = inflater_new.inflate(R.layout.group , null);
		}
		TextView text = (TextView)groupView.findViewById(R.id.group_name);
		ImageView image=(ImageView)groupView.findViewById(R.id.group_img);
		if(groupPosition == groupList.size()-1) {
			image.setImageResource(R.drawable.open_more);
		} else {
			if(isExpanded) {
				image.setImageResource(R.drawable.close_more);
			} else {
				image.setImageResource(R.drawable.open_more);
			}
		}
		groupView.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.FILL_PARENT, 70));
		if(groupPosition % 2 == 0) {
			groupView.setBackgroundResource(R.drawable.xml_linearlayout_background);
		} else {
			groupView.setBackgroundResource(R.drawable.xml_linearlayout_background2);
		}
		
		text.setText(groupList.get(groupPosition).getGroupName());

		return groupView;
	}

	public boolean hasStableIds() {
		return false;
	}

	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return false;
	}
 
}
